import pygame
from pygame.locals import *
import os

import general_utilities

class Sound_info:
  use_sound = 0
  sounds = {}
  
  def initialise(self,use_sound,dir):
    self.use_sound = use_sound
    self.data_dir = dir    
    if self.use_sound:
      import pygame.mixer
      pygame.mixer.init()
      for filename in os.listdir(self.data_dir):
        if filename.find(".wav")>=0:
          prefix = (filename[:filename.find(".")]).lower()
          sound = pygame.mixer.Sound(self.data_dir + "/" + filename)
          self.sounds[prefix] = sound

  def play(self,prefix,volume_mult=1):
    # supply volume_mult in [0,1]
    if not self.use_sound:
      return
    if prefix.lower() in self.sounds:
      volume = max(0,min(1,volume_mult))
      self.sounds[prefix.lower()].set_volume(volume)
      self.sounds[prefix.lower()].play(0)
      
sound = Sound_info()
